package com.service.socket.process;

import org.jboss.netty.channel.Channel;

import com.service.bean.device.HookArmCase;
import com.service.bean.signal.GpsHookArmCaseSignal;
import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.socket.protocol.CmdWord;
import com.service.socket.protocol.Conversion;
import com.service.socket.protocol.Packet;

/**
 * 勾臂车前端获取实时数据命令
 * @author lj
 *
 */
public class R_Class_E1 extends R_Class_Abstract {

	public R_Class_E1(Channel channel, Packet packet) {
		super(channel, packet);
	}

	@Override
	public void execute() {
		try {
			if (!packet.isCrcValid()) {
				packet.status = CmdWord.STATUS_FAIL;
				packet.dataArea = null;
				SendCommand.replyDtu(channel,packet);
				Logger.err("device [" + packet.getDeviceNo()+ "] regist fail on [isCrcValid]:" + packet.isCrcValid());
				return;
			}
			String mes = null;
			String companyCodes = new String(Conversion.hexStringToBytes(packet.dataArea));
			String[] codes = companyCodes.split(",");
			if ("01".equals(packet.cmdWord)) {
				mes = method_01(codes);
			} else if ("02".equals(packet.cmdWord)) {
				mes = method_02(codes);
			}
			mes = Conversion.bytesToHexString(mes.getBytes("UTF-8"));
			Packet bean = (Packet) packet.clone();
			if (mes != null) {
				bean.status = CmdWord.STATUS_SUCCESS;
				bean.dataArea = mes;
				SendCommand.replyDtu_E0_NOSAVE(channel,bean);
			} else {
				bean.status = CmdWord.STATUS_FAIL;
				bean.dataArea = null;
				SendCommand.replyDtu_E0_NOSAVE(channel, bean);
			}
		} catch (Exception e ) {
			Logger.err(e);
		}
	}
	
	/**
	 * 将定位数据解析成json, 传送给前端
	 * @return
	 */
	public String method_01(String[] companyCodes) {
		StringBuffer json = new StringBuffer();
		json.append("[");
		String x = null;
		GpsHookArmCaseSignal lastSignal = null;
		for (HookArmCase box : CacheManager.instance().hookArmCaseMap.values()) {
			for (String code : companyCodes) {
				if(box!= null && box.company.code.equals(code)){
					lastSignal = box.getHookArmLastGps_Received();
					if (null != lastSignal) {
						x = json.toString();
						if (x.equals("[")) {
							json.append("{" + lastSignal.toString() + "}");
						} else {
							json.append(",{" + lastSignal.toString() + "}");
						}
					}
				}
			}
		}
		json.append("]");
		return json.toString();
	}
	
	/**
	 * 将报警信息解析成json, 传送给前端
	 * @return
	 */
	public String method_02(String[] companyCodes) {
		StringBuffer json = new StringBuffer();
		json.append("[");
		String x = null;
		for (HookArmCase box : CacheManager.instance().hookArmCaseMap.values()) {
			for (String code : companyCodes) {
				if(box!= null && box.company.code.equals(code)){
					if (null != box.lastAlarm) {
						if (null != box.lastGps && 1 != box.lastGps.isGarbageFull)
							continue;
						x = json.toString();
						if (x.equals("[")) {
							json.append("{" + box.lastAlarm.toString() + "}");
						} else {
							json.append(",{" + box.lastAlarm.toString() + "}");
						}
					}
				}
			}
		}
		json.append("]");
		return json.toString();
	}
	
}
